/*
 * BaseUI.cpp
 *
 *  Created on: Oct 27, 2012
 *      Author: johnny
 */

#include "BaseUI.h"

#include <QFile>
#include <QtUiTools/QUiLoader>

#include <boost/format.hpp>
#include <boost/filesystem.hpp>

namespace ui {

BaseUI::BaseUI(): QObject(NULL), formWidget(NULL), parent(NULL) {

}

BaseUI::~BaseUI() {
    if(formWidget)
        formWidget->close();
}

void BaseUI::reParent(QWidget *parent) {
    this->parent = parent;
}

bool BaseUI::setup(const char* uiFile) {
	QUiLoader loader;
	QFile designFile(uiFile);

	designFile.open(QFile::ReadOnly);
	if (!designFile.isOpen()) {
        std::cerr << "CWD: " << boost::filesystem::current_path() << std::endl;
        core::Logger::getInstance().add(core::Alert,
                (boost::format("Failed to load %1% file") % uiFile).str());
		return false;
	}
	formWidget = loader.load(&designFile, NULL);
	designFile.close();

    if(parent != NULL)
       formWidget->setParent(parent);

	return true;
}

QWidget* BaseUI::getWidget() {
	return formWidget;
}

} /* namespace ui */
